*TITLE:INCUMBENT TENURE CROWDS OUT ECONOMIC VOTING
*JOURNAL: BRITISH JOURNAL OF POLITICAL SCIENCE
*AUTHOR: MARTIN VINÆS LARSEN
*FILE: INDIVIDUAL-LEVEL ANALYSES
*DEPENDENCIES: eststo, lmoments, blindschemes, combomarginsplot

*loading dataset*
use "individual.dta", clear

//DATA DESCRIPTION IN MANUSCRIPT//

*Median is 4 and mean is 5.
su time, d

//TABLE 2//

*estimating models
label var gov "%dontpost"
eststo a: melogit gov c.econ##c.time lgov ideology religion class || group: econ, vce(cluster ccode)  intpoints(4) intmethod(mcaghermite)
eststo b: melogit gov c.eco##(c.time) lgov class religion ideology  i.leader|| group: econ , vce(cluster ccode)  intpoints(4) intmethod(mcaghermite)
eststo c: logit gov c.eco#(c.time) c.econ  lgov class religion ideology i.leader i.group, vce(cluster ccode) 

*writing table
esttab a b c using microtable.tex, replace keep(econ time c.econ#c.time lgov ideology religion class) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(N, label("Observations") fmt(%9.0fc) )  ///
label se  title(Multi-level Logit Model of Voting for the Executive Party} \footnotesize \label{table:micro) ///
nonotes addnotes("Standard errors clustered by country  in parentheses." "Tenure omitted in model (3) due to collinearity with Survey FE.") nogaps  nomtitles noeqlines


//FIGURE 2//
*restoring estimates model
estimates restore c
*drawing figure
margins, dydx(econ) at(time=(1(1)15)) noestimcheck post coeflegend
marginsplot, level(95) scheme(plotplain)   ///
addplot(histogram time if e(sample)==1 & time < 16, yaxis(2) yscale(off axis(2)) percent  ylabel(0 60, axis(2))  ///
discret below lcolor(black*0.5) fcolor(white) ylabel(0(0.05)0.2, axis(1)) xlabel(0(4)16, labsize(medlarge)) ) ///
ytitle("Effect of NEP" " ", size(large)) xtitle(" " "Years in Office", size(large)) title(" ") legend(off)  ///
plotopts(lwidth(thick))   recast(line) recastci(rline) 
graph export fig2abe.eps, replace

*Testing for difference in average marginal effects.
test _b[1bn._at]==_b[15._at]

////////////////APPENDIX///////////////////////////////

*Sample Description (S2)
eststo hm: estpost tab country year
esttab hm using sampledesilevel.tex, unstack nomtitles replace ///
title(Observations Included in the Individual-level Analysis} ///
 \footnotesize \label{sammic) nonum noobs nonotes



*Descriptive statistics (S3)
preserve
label var gov "Executive party vote" 
label var lgov "Executive party vote (last time)" 
label var time "Time in office (years)" 
label var econ "National economic perceptions" 
label var class "Class" 
label var religion "Religiosity" 
label var ideology "Ideology" 
label var year "Survey year" 
file open anyname using deskriptivmik.txt, write text replace
file write anyname  _newline  _col(0)  "\begin{table} [htbp] \centering \caption{Descriptive Statistics, Individual-level Data} \label{desindi} \footnotesize \begin{tabular}{l*{5}{c}} \hline\hline"
file write anyname _newline _col(0) "&Mean & SD & Min& Max& n \\ \hline "
foreach x of varlist gov lgov  eco procon time class religion ideology  gr inf unem govoth   {
su `x' , d
file write anyname _newline (`"`: var label `x''"') "  &" _tab %9.2f  (r(mean)) " &" _tab %9.2f (r(sd)) " &" _tab %9.2f  (r(min)) " &" _tab %9.2f  (r(max)) " &" _tab %9.0f  (r(N)) " \\"
}
file write anyname _newline _col(0) "\hline\hline"
file write anyname _newline _col(0) "\end{tabular}"
file write anyname _newline _col(0) "\end{table}"
file close anyname
restore


*Sensitivity to Outliers (S6)
tempfile `temp1'
postfile results id m1 m2 m3 using `temp1'.dta, replace
forvalues i=0/60{
qui{
melogit gov c.eco##c.time lgov ideology religion class if group!=`i' || group:
  local b1=_b[c.econ#c.time]
melogit gov c.eco##c.time  lgov class religion ideology  i.leader if group!=`i'
local b2=_b[c.econ#c.time]
logit gov c.eco#c.time c.econ  lgov class religion ideology i.group if group!=`i'
 local b3=_b[c.econ#c.time]
post results (`i') (`b1') (`b2') (`b3')
}
di `i'
}
postclose results
preserve
use `temp1'.dta, clear
reshape long m, j(modelnr) i (id) 
gen pipe="|"
twoway scatter modelnr m if id!=0, msym(Oh) mcolor(black*0.7) msize(medlarge) ///
|| scatter modelnr m if id==0, scheme(plotplain) msym(none) mlab(pipe) mlabsize(vlarge) msize(large) ///
 xlabel(-0.15(0.03)0.00, labsize(medlarge)) ///
 ylabel(0.75 " " 1 "Model 1 - Survey RE " 2 "Model 2 - Leader FE " ///
 3 "Model 3 -Survey FE " 3.25 " ", labsize(medlarge) notick angle(0)) legend(off) ///
 xtitle(" " "Estimated interaction (Individual-level)", size(medlarge)) ytitle(" ")
 graph export apd_outlierilvl.eps, replace
restore
 
 
*Interaction checks (S7) 
lmoments time
tabstat time, stats(p50) by(trich)
recode trich 0=2 1=6 2=12
gen time2=time-2 if trich==2
replace time2=time-6 if trich==6
replace time2=time-12 if trich==12
tempfile temp1 temp2
logit gov c.econ##trich lgov ideology religion class i.group, vce(cluster ccode)
margin, dydx(econ) at(trich=(2 6 12)) saving(`temp1') level(95) noestimcheck
logit gov c.econ##c.time lgov ideology religion class i.group, vce(cluster ccode)  
margin, dydx(econ) at(time=(1(1)15)) saving(`temp2') level(95) noestimcheck 
preserve
use `temp1', clear
append using `temp2'
gen id=0
replace id=1 if _n >3
twoway line  _margin _at2 if id==1, lwidth(medthick) || ///
scatter _margin _at2 if id==0, msym(O) msize(medlarge) mcolor(black) || ///
rline _ci_lb _ci_ub _at2 if id==1, lcolor(black) || ///
rspike _ci_lb _ci_ub _at2 if id==0, scheme(plotplain) lcolor(black) yline(0) ///
legend(off) xtitle(Years in office, size(medlarge)) ytitle(Effect of NEP, size(medlarge))
graph export apd_ilevelhein.eps, replace
restore

*Coalition ctr. Single Party Gov's (S9)
label var gov "%dontpost"
eststo a: melogit gov c.econ##c.time lgov ideology religion class if govoth==0 || group: econ, vce(cluster ccode)  intpoints(4) intmethod(mcaghermite)
eststo b: melogit gov c.eco##(c.time)  lgov class religion ideology  i.leader if govoth==0 || group: econ , vce(cluster ccode)  intpoints(4) intmethod(mcaghermite)
eststo c: logit gov c.eco#(c.time) c.econ  lgov class religion ideology i.leader i.group if govoth==0, vce(cluster ccode) 
eststo d: melogit gov c.econ##c.time lgov ideology religion class if govoth==1 || group: econ, vce(cluster ccode)  intpoints(4) intmethod(mcaghermite)
eststo e: melogit gov c.eco##(c.time)  lgov class religion ideology  i.leader if govoth==1 || group: econ , vce(cluster ccode)  intpoints(4) intmethod(mcaghermite)
eststo f: logit gov c.eco#(c.time) c.econ  lgov class religion ideology i.leader i.group if govoth==1, vce(cluster ccode) 
esttab a b c d e f using apddualmic.tex, replace keep(econ time c.econ#c.time lgov ideology religion class) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(N, label("Observations") fmt(%9.0fc) ) ///
label se  title(Differences Between Single-Party and Multi-Party Governments in the Individual-level Data} \footnotesize \label{table:dualmic) ///
nonotes addnotes("Standard errors clustered by country in parentheses." "Tenure omitted in model (3) and (6) due to collinearity with Survey FE.") nogaps  nomtitles noeqlines



*Two-level Estimation methods (S11)
preserve
melogit gov c.eco lgov ideology religion class || group: econ, vce(cluster ccode)   intpoints(4) intmethod(mcaghermite)
predict r_c1 r_v2, reffects reses(r_cse1 r_cse2)
replace r_c1=r_c1+_b[econ]
keep r_c1 ccode year time group  r_cse1 
collapse (mean) r_c1 ccode year time r_cse1 , by(group)
gen ubc=r_c1+r_cse1*1.96
gen lbc=r_c1-r_cse1*1.96
gen yio3=time + runiform()/3-1/6
twoway lfitci r_c1 time, lwidth(medthick) || rspike ubc lbc yio3 || scatter r_c1 yio3, scheme(plotplain) legend(off) msym(Oh) ylab(-2 0 2 4, labsize(medlarge)) yline(0) mcolor(black) xlab(0(5)15, labsize(medlarge)) ytitle("Logit coefficient" " ", size(large)) xtitle(" " "Time in Office", size(large))
graph export ap3.eps, replace
*significant effects
reg r_c1 time, vce(cluster ccode)
restore


*Prospective economic conditions (S12)
label var gov "%dontpost"
eststo a: melogit gov (c.procon)##c.time lgov ideology religion class || group:, vce(cluster ccode)  intpoints(4) intmethod(mcaghermite)
eststo b: melogit gov (c.procon)##c.time  lgov class religion ideology  i.leader|| group:, vce(cluster ccode)   intpoints(4) intmethod(mcaghermite)
eststo c: logit gov (c.procon)#c.time  c.procon  lgov class religion ideology i.leader i.group, vce(cluster ccode)
esttab a b c using apdprocon.tex, replace keep(time procon c.procon#c.time lgov ideology religion class) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(N, label("Observations") fmt(%9.0fc) ) ///
label se  title(Prospective Economic Perceptions and Support for the Executive Party} \footnotesize \label{table:procon) ///
nonotes addnotes("Standard errors clustered by country in parentheses." "Tenure omitted in model (3) due to collinearity with Survey FE.") nogaps  nomtitles noeqlines


*Adressing Endogeneity (S13)

*No government partisans (i.e., voted gov last time).
label var gov "%dontpost"
eststo a: melogit gov c.eco##c.time  ideology religion class if lgov==0 || group:, vce(cluster ccode)  intpoints(4) intmethod(mcaghermite)
eststo b: melogit gov c.eco##(c.time)  class religion ideology  i.leader if lgov==0|| group:, vce(cluster ccode)  intpoints(4) intmethod(mcaghermite)
eststo c: logit gov c.eco#(c.time) c.econ  class religion ideology  i.leader i.group if lgov==0 , vce(cluster ccode) 

*Figure wo/ partisans  (i.e., voted gov last time).
logit gov c.eco#(c.time) c.econ  lgov class religion ideology i.leader i.group if lgov==0, vce(cluster ccode)  
margins, dydx(econ) at(time=(1(1)15)) 
marginsplot, level(95) scheme(plotplain) ///
addplot(histogram time if e(sample)==1 & time < 16, yaxis(2) yscale(off axis(2)) percent  ylabel(0 60, axis(2))  ///
discret below lcolor(black*0.5) fcolor(white) ylabel(0(0.05)0.2, labsize(medlarge) axis(1)) xlabel(0(4)16, labsize(medlarge)) ) ///
recast(line) recastci(rline) ///
ytitle("Effect of NEP" " ", size(large)) xtitle(" " "Years in Office", size(large)) title(" ") legend(off)  ///
plotopts(lwidth(thick))
graph export fig_nopartisan.eps, replace

*Instrumental Variable
tempvar cons
gen `cons'=1
eststo d: ivregress 2sls gov (c.econ c.econ#c.time= c.unem inf gr (c.unem c.inf c.gr)#c.time) time `cons', first nocons
*Table endogeneity
esttab a b c d using microtable_adj.tex, replace keep(econ time c.econ#c.time ideology religion class) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(N, label("Observations") fmt(%9.2fc %9.2fc %9.0fc) ) ///
label se  title(Adressing Endogeneity Problems in the Individual-level Data} \footnotesize \label{table:microadj) ///
nonotes addnotes("Standard errors clustered in parentheses. Clustered at the country-level for columns one, two and three." "Tenure omitted in model (3) due to collinearity with Survey FE.") nogaps  nomtitles noeqlines

*Figure based on instrumental variables regression.
est restore d
margins, dydx(econ) at(time=(1(1)15))
marginsplot, level(95) scheme(plotplain)  recast(line) recastci(rline) ///
addplot(histogram time if e(sample)==1 & time < 16, yaxis(2) yscale(off axis(2)) percent  ylabel(0 60, axis(2))  ///
discret below lcolor(black*0.5) fcolor(white) yline(0) ylabel(0(0.2)0.8 , labsize(medlarge)  axis(1)) xlabel(0(4)16, labsize(medlarge)) ) ///
ytitle("Effect of NEP" " ", size(large)) xtitle(" " "Years in Office", size(large)) title(" ") legend(off)  ///
plotopts(lwidth(thick))
graph export fig_instrument.eps, replace

